#Boa:Dialog:Dialog1

import wx
import wx.grid
import consultasBD

def create(parent):
    return Dialog1(parent)

[wxID_DIALOG1, wxID_DIALOG1CHECKBOX1, wxID_DIALOG1CONSULTAR, 
 wxID_DIALOG1DATEPICKERCTRL1, wxID_DIALOG1DATEPICKERCTRL2, wxID_DIALOG1GRID1, 
 wxID_DIALOG1GRID2, wxID_DIALOG1STATICLINE1, wxID_DIALOG1STATICTEXT1, 
 wxID_DIALOG1STATICTEXT2, wxID_DIALOG1STATICTEXT3, 
] = [wx.NewId() for _init_ctrls in range(11)]

class Dialog1(wx.Dialog):
    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Dialog.__init__(self, id=wxID_DIALOG1, name='', parent=prnt,
              pos=wx.Point(4, 52), size=wx.Size(981, 376),
              style=wx.DEFAULT_DIALOG_STYLE, title='Estadisticas de Venta')
        self.SetClientSize(wx.Size(965, 340))

        self.grid1 = wx.grid.Grid(id=wxID_DIALOG1GRID1, name='grid1',
              parent=self, pos=wx.Point(8, 88), size=wx.Size(488, 240),
              style=0)

        self.checkBox1 = wx.CheckBox(id=wxID_DIALOG1CHECKBOX1,
              label='Por Email', name='checkBox1', parent=self,
              pos=wx.Point(232, 8), size=wx.Size(70, 13), style=0)
        self.checkBox1.SetValue(False)

        self.datePickerCtrl1 = wx.DatePickerCtrl(id=wxID_DIALOG1DATEPICKERCTRL1,
              name='datePickerCtrl1', parent=self, pos=wx.Point(72, 8),
              size=wx.Size(96, 21), style=wx.DP_DROPDOWN | wx.DP_SHOWCENTURY)

        self.datePickerCtrl2 = wx.DatePickerCtrl(id=wxID_DIALOG1DATEPICKERCTRL2,
              name='datePickerCtrl2', parent=self, pos=wx.Point(72, 40),
              size=wx.Size(96, 21), style=wx.DP_DROPDOWN | wx.DP_SHOWCENTURY)

        self.staticText1 = wx.StaticText(id=wxID_DIALOG1STATICTEXT1,
              label='Desde', name='staticText1', parent=self, pos=wx.Point(16,
              8), size=wx.Size(31, 13), style=0)

        self.staticText2 = wx.StaticText(id=wxID_DIALOG1STATICTEXT2,
              label='Hasta:', name='staticText2', parent=self, pos=wx.Point(16,
              40), size=wx.Size(33, 13), style=0)

        self.consultar = wx.Button(id=wxID_DIALOG1CONSULTAR, label='Consultar',
              name='consultar', parent=self, pos=wx.Point(224, 32),
              size=wx.Size(91, 32), style=0)
        self.consultar.Bind(wx.EVT_BUTTON, self.OnConsultarButton,
              id=wxID_DIALOG1CONSULTAR)

        self.staticLine1 = wx.StaticLine(id=wxID_DIALOG1STATICLINE1,
              name='staticLine1', parent=self, pos=wx.Point(504, 8),
              size=wx.Size(8, 336), style=0)

        self.staticText3 = wx.StaticText(id=wxID_DIALOG1STATICTEXT3,
              label='Mismo Periodo A\xf1o Anterior', name='staticText3',
              parent=self, pos=wx.Point(544, 24), size=wx.Size(133, 13),
              style=0)

        self.grid2 = wx.grid.Grid(id=wxID_DIALOG1GRID2, name='grid2',
              parent=self, pos=wx.Point(520, 88), size=wx.Size(440, 240),
              style=0)

    def __init__(self, parent):
        self._init_ctrls(parent)
        self.padre = parent
        
        self.grid1.CreateGrid(9,5)
        self.grid2.CreateGrid(9,5)
        
        self.grid1.SetColLabelValue(0," X Vendedora ...")
        self.grid2.SetColLabelValue(0," X Vendedora  ...")
        self.grid1.SetColLabelValue(1," TOTALES ...")
        self.grid2.SetColLabelValue(1," TOTALES ...")
        self.grid1.SetColLabelValue(2," PESOS ...")
        self.grid2.SetColLabelValue(2," PESOS ...")
        self.grid1.SetColLabelValue(3," TARJETAS ...")
        self.grid2.SetColLabelValue(3," TARJETAS ...")
        self.grid1.SetColLabelValue(4," C.C ...")
        self.grid2.SetColLabelValue(4," C.C ...")
        
        if self.padre.loginRol == "cajera":
            self.checkBox1.Enable(True)
        else: 
            self.checkBox1.Enable(False)
        
                
        self.grid1.AutoSizeColumn(0,True)
        self.grid1.AutoSizeColumn(1,True)
        self.grid1.AutoSizeColumn(2,True)
        self.grid1.AutoSizeColumn(3,True)
        self.grid1.AutoSizeColumn(4,True)
        self.grid2.AutoSizeColumn(0,True)
        self.grid2.AutoSizeColumn(1,True)
        self.grid2.AutoSizeColumn(2,True)
        self.grid2.AutoSizeColumn(3,True)
        self.grid2.AutoSizeColumn(4,True)
        
        
    def OnConsultarButton(self, event):
        self.limpiaGrilla()
        en1y2= "" 
        valor = self.checkBox1.Value
        if valor:
            formaticket=2
            en1y2= "(idformaticket=2 OR idformaticket=1)"
        else:
             en1y2= "idformaticket=1"
        
        
        desde = str(self.datePickerCtrl1.GetValue())[:8]
        hasta = str(self.datePickerCtrl2.GetValue())[:8]

        
        desde = self.formatear_fecha_date2(desde)
        hasta = self.formatear_fecha_date2(hasta)
        
        
        
        
        SQL="SELECT b.nombre AS 'Nombre', SUM( totalfactura ) AS 'Total', (SELECT sum( monto )FROM ventastienepagos c WHERE idformapagos = 'P' and c.vendedora = b.idvendedora and c.vendedora=a.usuario AND (fecha>='%s' AND fecha<='%s')) AS Pesos,(SELECT sum( monto) FROM ventastienepagos c WHERE idformapagos <>'P' and c.vendedora = b.idvendedora and c.vendedora=a.usuario AND (fecha>='%s' AND fecha<='%s')) AS Tarjetas FROM ventas a, vendedoras b WHERE a.usuario = b.idvendedora AND (a.fecha>='%s' AND a.fecha<='%s') AND totalfactura>0 AND idformaticket!=999 AND %s GROUP BY Nombre ORDER BY Total DESC"    %(desde,hasta, desde,hasta,desde,hasta,en1y2)  
        print SQL
        resultados = consultasBD.ordenCrudaDict(SQL)
        i =0 
        for r in resultados:
            total = float("0.0")
            pesos = float("0.0")
            tarjetas = float("0.0")
            ctacte = float("0.0")
            print r["Total"]
            print r["Pesos"]
            print r["Tarjetas"]
            if r["Nombre"] != None:
                self.grid1.SetCellValue(i, 0, str(r["Nombre"]))
            
            if r["Total"] != None:
                total = round(r["Total"],2)
                self.grid1.SetCellValue(i, 1, str(round(r["Total"],2)))
                
            if r["Pesos"] != None :
                pesos = round(r["Pesos"],2)
                self.grid1.SetCellValue(i, 2, str(round(r["Pesos"],2))) 
            
            if r["Tarjetas"] != None :
                tarjetas = round(r["Tarjetas"],2)
                self.grid1.SetCellValue(i, 3, str(round(r["Tarjetas"],2))) 

            
            i = i + 1
        
        
        #self.grid1.SetCellValue(i, 4,   ) #
        total1 = float("0.0")
        total2 = float("0.0")
        total3 = float("0.0")
        total = float("0.0")
        for r in xrange(i):
            if self.grid1.GetCellValue(r,1) != "":
                total1 = total1 + float(self.grid1.GetCellValue(r,1))
            if self.grid1.GetCellValue(r,2) != "":
                total2 = total2 + float(self.grid1.GetCellValue(r,2))
            if self.grid1.GetCellValue(r,3) != "":
                total3 = total3 + float(self.grid1.GetCellValue(r,3))
            if self.grid1.GetCellValue(r,4) != "":
                total = total + float(self.grid1.GetCellValue(r,4))
        
        
        self.grid1.SetCellValue(i ,0,"TOTALES ---->")
        self.grid1.SetCellValue(i ,1,str(total1))
        self.grid1.SetCellValue(i ,2,str(total2))
        self.grid1.SetCellValue(i ,3,str(total3))
        self.grid1.SetCellValue(i ,4,str(total))
        
        
    def limpiaGrilla(self):
            for x in xrange(8):
                for y in xrange(5):
                    self.grid1.SetCellValue(x,y, "")

        
    def formatear_fecha_date2(self,fecha):
        aux = str(fecha).split(" ")[0].split("/")
        return "20%s-%s-%s" % (aux[2], aux[0], aux[1]) 
